package com.hui.account.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hui.account.R;
import com.hui.account.database.SQLManage;
import com.hui.account.dialog.ImageChoiceDialog;
import com.hui.account.entity.FileEntity;
import com.hui.account.entity.RecordEntity;
import com.hui.account.entity.UserEntity;
import com.hui.account.utils.ButtonUtils;
import com.hui.account.utils.CommonUtils;
import com.hui.account.utils.ImageUtils;
import com.hui.account.utils.TimeUtils;
import com.hui.account.utils.ToastUtils;

public class RecordRecoverActivity extends AppCompatActivity implements View.OnClickListener {

    Context context;

    ImageView typeIcon, imageView;

    RecordEntity record;

    Button backBtn, recoverBtn, delBtn;

    TextView statusText, createTimeText, payTimeText;

    EditText nameEdit, moneyEdit, payEdit, remarkEdit;

    SharedPreferences preferences; // preferences：共享参数，存储图片

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        setContentView(R.layout.activity_record_recover);

        // 1、设置屏幕顶部字体颜色
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        // 2、页面初始化
        initView();

        // 3、preferences：共享参数初始化，清空共享参数值
        preferences = getSharedPreferences("shareParam", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("modImageTemp", "");
        edit.commit();

        String value = preferences.getString("selectRecord", "");
        if (!TextUtils.isEmpty(value)) {
            record = new Gson().fromJson(value, RecordEntity.class);
            UserEntity user = SQLManage.getUserById(record.getUserId());
            typeIcon.setImageResource(record.getIcon());
            nameEdit.setText(null == user ? "--" : user.getUserName());
            moneyEdit.setText(CommonUtils.MONEY + record.getMoney());
            createTimeText.setText(record.getTime());
            remarkEdit.setText(record.getRemark());
            payEdit.setText(record.getPay());
            payTimeText.setText(TimeUtils.getStringToTimestamp(record.getPayTime(), TimeUtils.TIME_SQL_PATTERN_TIME));
            if (record.getType() == 0) {
                statusText.setText("未还");
                statusText.setTextColor(context.getResources().getColor(R.color.red_FF5A5A));
                moneyEdit.setTextColor(context.getResources().getColor(R.color.red_FF5A5A));
            }

            if (!TextUtils.isEmpty(record.getImageName())) {
                // 根据图片名称查询图片路径
                FileEntity file = SQLManage.getFileByName(record.getImageName());
                if (null != file && !TextUtils.isEmpty(file.getPath())) {
                    Bitmap bitmap = ImageUtils.getDiskBitmap(file.getPath());
                    if (null != bitmap) {
                        imageView.setImageBitmap(bitmap);
                        edit.putString("modImageTemp", new Gson().toJson(bitmap));
                        edit.commit();
                    }
                }
            }
        } else {
            ToastUtils.error(context, "欠款单信息获取失败", Gravity.CENTER, 0, 0);
            finish();
        }
    }

    private void initView() {
        typeIcon = findViewById(R.id.recover_record_type_icon);
        backBtn = findViewById(R.id.recover_record_back);
        delBtn = findViewById(R.id.recover_record_del);
        recoverBtn = findViewById(R.id.recover_record_recover);
        statusText = findViewById(R.id.recover_record_status);
        payTimeText = findViewById(R.id.recover_record_pay_time);
        createTimeText = findViewById(R.id.recover_record_create_time);
        remarkEdit = findViewById(R.id.recover_record_remark);
        moneyEdit = findViewById(R.id.recover_record_money);
        payEdit = findViewById(R.id.recover_record_pay);
        nameEdit = findViewById(R.id.recover_record_name);
        imageView = findViewById(R.id.recover_record_image);

        delBtn.setOnClickListener(this);
        backBtn.setOnClickListener(this);
        imageView.setOnClickListener(this);
        recoverBtn.setOnClickListener(this);
    }

    /**
     * 还原
     */
    private void recoverRecord() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确定还原").setMessage("是否确认还原该笔账单？").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                SQLManage.recoverRecord(record.getId());
                ToastUtils.success(context, "还原成功", Gravity.CENTER, 0, 0);
                finish();
            }
        });
        builder.create().show();
    }

    /**
     * 删除
     */
    private void delRecord() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确定删除").setMessage("是否确认删除该笔订单，永久删除？").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                // 1、删除图片
                if (!TextUtils.isEmpty(record.getImageName())) {
                    FileEntity file = SQLManage.getFileByName(record.getImageName());
                    if (null != file) {
                        SQLManage.delFile(file.getId());
                        ImageUtils.delDiskFile(file.getPath());
                    }
                }

                // 2、删除欠款单
                SQLManage.delRecord(record.getId());
                ToastUtils.success(context, "删除成功", Gravity.CENTER, 0, 0);
                finish();
            }
        });
        builder.create().show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.recover_record_back:
                finish();
                break;
            case R.id.recover_record_del:
                if (!ButtonUtils.isFastDoubleClick())
                    delRecord();

                break;
            case R.id.recover_record_recover:
                if (!ButtonUtils.isFastDoubleClick())
                    recoverRecord();

                break;
            case R.id.recover_record_image:
                ImageChoiceDialog dialog = new ImageChoiceDialog(this);
                dialog.show();
                dialog.setDialogSize();
                break;
        }
    }
}